﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for EmployeeService
    /// </summary>
    public class EmployeeService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            var obj = new OTS_EMPLOYEE();
            if (request["oper"] == "del")
            {
                var id = request["id"];
                var delcmd = new Employee_Service();
                if (id != string.Empty)
                { 
                    delcmd.Delete(id);
                }
            }
            var _cmd = new Employee_Service();
            var list = new List<OTS_EMPLOYEE>();
            var result = _cmd.GetAll().OrderByDescending(x=> x.CREATED).ToList();
            foreach (OTS_EMPLOYEE o in result)
            {
                obj = new OTS_EMPLOYEE();
                obj.ID = o.ID;
                obj.TITLE = o.TITLE;
                obj.NAME_EN = o.NAME_EN;
                obj.NAME_TH = o.NAME_TH;
                obj.POSITION = o.POSITION;
                obj.NICK_NAME = o.NICK_NAME;
                obj.GENDER = o.GENDER;
                obj.ID_CARD = o.ID_CARD;
                obj.POSITION = o.POSITION;
                obj.DATEOFBIRTH = o.DATEOFBIRTH;
                obj.NATIONALITY = o.NATIONALITY;
                obj.RELIGION = o.RELIGION;
                obj.STARTWORK = o.STARTWORK;
                obj.ENDWORK = o.ENDWORK;

                list.Add(obj);
            }
            list = list.OrderByDescending(x => x.CREATED).ToList();
            var sortcol = request["sidx"];
            var sortorder = request["sord"];

           var lst = SortOrder(list, sortcol, sortorder);

           int total = lst.Count == 0 ? 1 : (int)Math.Ceiling((double)lst.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = lst.Count,
                rows = lst.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        private List<OTS_EMPLOYEE> SortOrder(List<OTS_EMPLOYEE> list, string sortCol, string sortOrder)
        {
            var result = new List<OTS_EMPLOYEE>();
            if (sortCol == "ID")
            {
                if (sortOrder == "asc")
                {
                    result = list.OrderBy(x => x.ID).ToList();
                }
                else if (sortOrder == "desc")
                {
                    result = list.OrderByDescending(x => x.ID).ToList();
                }
            }
            else if (sortCol == "TITLE")
            {
                if (sortOrder == "asc")
                {
                    result = list.OrderBy(x => x.TITLE).ToList();
                }
                else if (sortOrder == "desc")
                {
                    result = list.OrderByDescending(x => x.TITLE).ToList();
                }
            }
            else if (sortCol == "NAME_EN")
            {
                if (sortOrder == "asc")
                {
                    result = list.OrderBy(x => x.NAME_EN).ToList();
                }
                else if (sortOrder == "desc")
                {
                    result = list.OrderByDescending(x => x.NAME_EN).ToList();
                }
            }
            else if (sortCol == "NAME_TH")
            {
                if (sortOrder == "asc")
                {
                    result = list.OrderBy(x => x.NAME_TH).ToList();
                }
                else if (sortOrder == "desc")
                {
                    result = list.OrderByDescending(x => x.NAME_TH).ToList();
                }
            }
            else
            {
                result = list;
            }

            return result;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}